<#assign base=request.contextPath />
<!DOCTYPE html>
<div class="row">
	<div class="col-md-12 col-sm-12 col-xs-12">
		<div class="x_panel">
			<div class="x_title">
				<h2>菜单编辑 <small></small></h2>
				<div class="clearfix"></div>
			</div>
			<div class="x_content">
				<br/>
				<form id="postForm" data-parsley-validate class="form-horizontal form-label-left">
					<input type="hidden" name="id" value="${(obj.id)!''}">
                    <input type="hidden" name="createTime" id="createTime" value="${(obj.createTime)!.now?string['yyyy-MM-dd HH:mm:ss']}">
                    <input type="hidden" name="updateTime" value="${.now?string['yyyy-MM-dd HH:mm:ss']}">
                    <input type="hidden" name="creator" value="${(obj.creator)!admin.userName}">
					<div class="form-group">
						<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">菜单名称 <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                        	<input type="text" id="menuName" name="menuName" value="${(obj.menuName)!''}" class="form-control col-md-7 col-xs-12">
                        </div>
					</div>
					<div class="form-group">
						<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">菜单路径 <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                        	<input type="text" id="menuUrl" name="menuUrl" value="${(obj.menuUrl)!''}" class="form-control col-md-7 col-xs-12">
                        </div>
					</div>
					<div class="form-group">
						<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">排序 <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                        	<input type="text" id="sort" name="sort" value="${(obj.sort)!99}" class="form-control col-md-7 col-xs-12">
                        </div>
					</div>
					<#if obj??>
					<div class="form-group">
						<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">菜单层级 <span class="required"></span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                        	一级：<input type="radio" class="flat" name="menuLevel" value="1" <#if (obj.menuLevel==1)>checked="checked"</#if>/>&nbsp;
                        	二级：<input type="radio" class="flat" name="menuLevel" value="2" <#if (obj.menuLevel==2)>checked="checked"</#if>/>
                        </div>
					</div>
					<div class="form-group" style="display:none;" id="parentMenu">
						<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">父菜单 <span class="required"></span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                        	<select id="parentId" name="parentId" class="form-control select2">
	                        	<option value="">请选择</option>
	                        	<#list listParent as parent>
	                        	<option value="${parent.id?c}" <#if (obj.parentId==parent.id)>selected="selected"</#if>>${parent.menuName}</option>
	                        	</#list>
	                    	</select>
                        </div>
					</div>
					<#else>
					<div class="form-group">
						<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">菜单层级 <span class="required"></span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                        	一级：<input type="radio" class="flat" name="menuLevel" value="1" checked="checked"/>&nbsp;
                        	二级：<input type="radio" class="flat" name="menuLevel" value="2"/>
                        </div>
					</div>
					<div class="form-group" style="display:none;" id="parentMenu">
						<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">父菜单 <span class="required"></span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                        	<select id="parentId" name="parentId" class="form-control select2">
	                        	<option value="">请选择</option>
	                    	</select>
                        </div>
					</div>
					</#if>
					<div class="form-group">
						<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">菜单描述 <span class="required"></span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                        	<input type="text" id="menuDesc" name="menuDesc" value="${(obj.menuDesc)!''}" class="form-control col-md-7 col-xs-12">
                        </div>
					</div>
					<div class="form-group">
						<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">图标 <span class="required"></span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                        	<i class="fa fa-home"></i><input type="radio" class="flat" name="menuIcon" value="fa fa-home" checked="checked"/>&nbsp;
                        	<i class="fa fa-edit"></i><input type="radio" class="flat" name="menuIcon" value="fa fa-edit"/>&nbsp;
                        	<i class="fa fa-desktop"></i><input type="radio" class="flat" name="menuIcon" value="fa fa-desktop"/>&nbsp;
                        	<i class="fa fa-table"></i><input type="radio" class="flat" name="menuIcon" value="fa fa-table"/>&nbsp;
                        	<i class="fa fa-bar-chart-o"></i><input type="radio" class="flat" name="menuIcon" value="fa fa-bar-chart-o"/>&nbsp;
                        	<i class="fa fa-clone"></i><input type="radio" class="flat" name="menuIcon" value="fa fa-clone"/>&nbsp;
                        	<i class="fa fa-sitemap"></i><input type="radio" class="flat" name="menuIcon" value="fa fa-sitemap"/>&nbsp;
                        	<i class="fa fa-cog"></i><input type="radio" class="flat" name="menuIcon" value="fa fa-cog"/>&nbsp;
                        </div>
					</div>
					<div class="ln_solid"></div>
					<div class="form-group">
						<div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
							<button type="button" class="btn btn-success" id="submit">保存</button>
							<button type="button" class="btn btn-primary" id="goBack">返回</button>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript">
$(function() {
	//iCheck
	if ($("input.flat")[0]) {
		$(document).ready(function () {
	    	$('input.flat').iCheck({
	        	checkboxClass: 'icheckbox_flat-green',
	        	radioClass: 'iradio_flat-green'
	        });
	    });
	}
	// /iCheck
	//select2
	$(".select2").select2({width: "100%"});
	// /select2
	$("input:radio[name='menuLevel']").on('ifChecked', function(event){
		if($(this).val()=="1"){
			$("#parentMenu").hide();
		}
		else{
			$.post("${base}/common/getParentMenuList", {"menuLevel":$(this).val()}, function(data) {
				if (data.flag) {
					$("#parentId").empty();
					$.each(data.obj, function(i, n){
						$("#parentId").append("<option value='"+n.id+"'>"+n.menuName+"</option>");
					});
				}
			});
			$("#parentMenu").show();
		}
	});
	var layerIndex = "${(obj.menuLevel)!''}";
	if(layerIndex==""||layerIndex=="1"){
		$("#parentMenu").hide();
	}

	$("#goBack").click(function() {
		$("#main_content").load('${base}/gx/sysMenu/list');	        
    });
	
	$("#submit").click(function() {
	
		var menuName = $.trim($("#menuName").val());
	    var menuUrl = $.trim($("#menuUrl").val());
	    var sort = $.trim($("#sort").val());
	    if(menuName==''){
	    	swal("请输入菜单名称");
	        return;
	    }
	    if(menuUrl==''){
	        swal("请输入菜单路径");
	        return;
	    }
	    if(sort==''){
	        swal("请输入排序");
	        return;
	    }
	    if(!(/(^[1-9]\d*$)/.test(sort))){
	    	swal("请输入正确的排序号");
	        return;
	    }
        var url = "${base}/gx/sysMenu/save";
        $.post(url, $("#postForm").serialize(), function(data) {
            if (data.flag) {
            	swal(data.msg, "", "success");
                $("#main_content").load('${base}/gx/sysMenu/list');
            }else{
            	swal(data.msg, "", "error");
            }
        });
    });
});
</script>